package org.achartengine.tools;

import org.achartengine.GraphicalView;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class RangeSlider {
	private int xLeftSlider;

	private int xRightSlider;
	
	private int xLeftSliderLimit;
	private int xRightSliderLimit;
	
	
	private boolean yCordSet=false;
	private boolean xCordSet=false;
	
	private int yTopSlider;
	private int yBottomSlider;
	
	private boolean leftSliderTouched;
	private boolean rightSliderTouched;
	
	private Paint mPaint;
	private Rect mScreen;
	
//	Bitmap leftSliderImage;
//	Bitmap rightSliderImage;
	
	private Drawable leftSliderImage;
	private Drawable rightSliderImage;
	

	
	public RangeSlider(Context context) {
		this.setLeftSliderTouched(false);
		this.setRightSliderTouched(false);
		mPaint = new Paint();			
		mScreen = new Rect();
		mScreen.set(0, 0, 0, 0);		
		this.initializeCoordinates(mScreen);
	
		leftSliderImage =  context.getResources().getDrawable(org.achartengine.R.drawable.slider);
				//BitmapFactory.decodeStream(GraphicalView.class.getResourceAsStream("image/slider_left.png"));
		rightSliderImage = context.getResources().getDrawable(org.achartengine.R.drawable.slider); 
				//BitmapFactory.decodeStream(GraphicalView.class.getResourceAsStream("image/slider_right.png"));
		
	
	}

	private void initializeCoordinates(Rect mScreen){
		this.xLeftSlider = mScreen.left;
		this.xRightSlider = mScreen.right;
		this.yTopSlider = mScreen.top;
		this.yBottomSlider = mScreen.bottom;
		this.xLeftSliderLimit = mScreen.left;
		this.xRightSliderLimit = mScreen.right;
	}
	
	public void setYCoordinates(int yTop, int yBottom){
		if(!isyCordSet()){
			this.yTopSlider = yTop;
			this.yBottomSlider = yBottom;
		}
		setyCordSet(true);
	}
	

	public void setLeftSliderX(int x) {
		
		if(isLeftSliderTouched()){
			if((x + 15 < xRightSlider) && (x >= xLeftSliderLimit)){
				this.xLeftSlider= x;			
			}
		}
	}

	public void setRightSliderX(int x) {
	
		if(isRightSliderTouched()){
			if((xLeftSlider + 15 < x) && (x <= xRightSliderLimit)){
				this.xRightSlider= x;			
			}
		}		
	}
	

	public void setXCoordinates(int xLeft, int xRight, int xLeftLimit){
		
		if(!isxCordSet()){
			this.xLeftSlider = xLeft;
			this.xRightSlider = xRight;		
			this.xLeftSliderLimit = xLeftLimit;
			this.xRightSliderLimit = xRight;
		}
		setxCordSet(true);			
		
	}
	
	
	public void draw(Canvas canvas){
		
		//mPaint.setColor(Color.RED);
		//canvas.drawLine(xLeftSlider, yTopSlider, xLeftSlider, yBottomSlider, mPaint);
		
		//mPaint.setColor(Color.RED);
		//canvas.drawLine(xRightSlider, yTopSlider, xRightSlider, yBottomSlider, mPaint);		
		
		
		
		
		mPaint.setColor(Color.BLACK);
		mPaint.setAlpha(100);
		canvas.drawRect(xLeftSliderLimit, yTopSlider,xLeftSlider,yBottomSlider,mPaint);
		canvas.drawRect(xRightSlider, yTopSlider, xRightSliderLimit, yBottomSlider, mPaint);
		
		
		
		//int imageWidth=leftSliderImage.getScaledWidth(canvas);
		int imageWidth=leftSliderImage.getMinimumWidth();
		//int imageWidth=20;
		mPaint.setAlpha(255);
		
		
		Rect dst = new Rect(xLeftSlider-imageWidth/2, yTopSlider-2, xLeftSlider+imageWidth/2, yBottomSlider+2);
		leftSliderImage.setBounds(dst);
		leftSliderImage.draw(canvas);
		//canvas.drawBitmap(leftSliderImage, null, dst, mPaint);
		dst.set(xRightSlider-imageWidth/2, yTopSlider-2, xRightSlider+imageWidth/2, yBottomSlider+2);
		rightSliderImage.setBounds(dst);
		rightSliderImage.draw(canvas);
		//canvas.drawBitmap(rightSliderImage, null, dst, mPaint);
		
		
		
		/*canvas.drawRect(xLeftSlider, yTopSlider, xRightSlider, (yTopSlider+yBottomSlider)/2, mPaint);
		mPaint.setAlpha(70);
		canvas.drawRect(xLeftSlider, (yTopSlider+yBottomSlider)/2, xRightSlider, yBottomSlider, mPaint);*/
		
	}
	
	public void handleTouch(int eventX, int eventY){
		if ((eventX >= (xLeftSlider - 5) && eventX <= (xLeftSlider + 5))
				&& (eventY >= yTopSlider && eventY <= yBottomSlider)) {
			//**setLeftSliderTouched(true);
			//**setRightSliderTouched(false);
			leftSliderTouched = true;
			rightSliderTouched = false;
			//Log.d(TAG, "Line is touched ");
		} else if ((eventX >= (xRightSlider - 5) && eventX <= (xRightSlider + 5))
				&& (eventY >= yTopSlider && eventY <= yBottomSlider)) {
			//**setRightSliderTouched(true);
			//**setLeftSliderTouched(false);
			leftSliderTouched = false;
			rightSliderTouched = true;			
		} else if ((eventX > (xLeftSlider+5)) && ( eventX < (xRightSlider-5))){			
			//**setLeftSliderTouched(true);
			//**setRightSliderTouched(true);
			leftSliderTouched = true;
			rightSliderTouched = true;			
		} else{
			//**setLeftSliderTouched(false);
			//**setRightSliderTouched(false);
			leftSliderTouched = false;
			rightSliderTouched = false;			
		}
	}


	public boolean isLeftSliderTouched() {
		return leftSliderTouched;
	}


	public void setLeftSliderTouched(boolean leftSliderTouched) {
		this.leftSliderTouched = leftSliderTouched;
	}


	public boolean isRightSliderTouched() {
		return rightSliderTouched;
	}


	public void setRightSliderTouched(boolean rightSliderTouched) {
		this.rightSliderTouched = rightSliderTouched;
	}

	public boolean isyCordSet() {
		return yCordSet;
	}

	public void setyCordSet(boolean yCordSet) {
		this.yCordSet = yCordSet;
	}

	public boolean isxCordSet() {
		return xCordSet;
	}

	public void setxCordSet(boolean xCordSet) {
		this.xCordSet = xCordSet;
	}
	
	public int getxLeftSlider() {
		return xLeftSlider;
	}

	public int getxRightSlider() {
		return xRightSlider;
	}

	public int getLeftRightDiff() {
		return (xRightSlider-xLeftSlider);
		
	}

	public void reset() {
		// TODO Auto-generated method stub
		this.xLeftSlider =  this.xLeftSliderLimit;
		this.xRightSlider = this.xRightSliderLimit;
		
	}

	
}
